using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;


namespace CsvCatcher.Setup
{
    public class SettingsDAO
    {
        private DataSchema.CatcherSettings _settings;
        private string _settPath;
        public SettingsDAO(DataSchema.CatcherSettings sett)
        {
            _settings = sett;
            string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            _settPath = System.IO.Path.Combine(System.Windows.Forms.Application.StartupPath, HunterPluginSDK.Constants.PlugInsSettDir);
            _settPath = System.IO.Path.Combine (_settPath, assemblyName + HunterPluginSDK.Constants.PlugInSettExt);

        }

        public void Serialize()
        {
            XmlSerializer x = new XmlSerializer(typeof(DataSchema.CatcherSettings));

            using (TextWriter writer = new StreamWriter(_settPath))
            {
                x.Serialize(writer, _settings);
                writer.Close();
            }
        }

        public void Deserialize(ref DataSchema.CatcherSettings sett)
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(_settPath);
            if (fi.Exists)
            {
                XmlSerializer x = new XmlSerializer(typeof(DataSchema.CatcherSettings));
                using (FileStream file = new FileStream(_settPath, FileMode.Open))
                {
                    _settings = sett = (DataSchema.CatcherSettings)x.Deserialize(file);
                }
            }
            else
                _settings = sett = new CsvCatcher.DataSchema.CatcherSettings();
        }
    }
}
